#include "mainwindow.h"

#include "qstringlistmodel.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowClass())
{
    ui->setupUi(this);

    string_list << "京城" << "魔都" << "古都" << "神都";
    model_s_l = new QStringListModel(this);
    model_s_l->setStringList(string_list);
    ui->listView->setModel(model_s_l);
    //双击或选择时允许编辑
    ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_InitList_clicked()
{
    model_s_l->setStringList(string_list);
}

void MainWindow::on_pushButton_ListClear_clicked()
{
    //应当通过model操作
    model_s_l->removeRows(0, model_s_l->rowCount());
}

void MainWindow::on_checkBox_Editable_stateChanged(int state)
{
    if (state == Qt::Checked)
        ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
    else
        ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);//不允许编辑
}

void MainWindow::on_pushButton_ListAppend_clicked()
{
    model_s_l->insertRow(model_s_l->rowCount());

    //索引
    QModelIndex index = model_s_l->index(model_s_l->rowCount() - 1);
    model_s_l->setData(index, "new item append", Qt::DisplayRole);
    ui->listView->setCurrentIndex(index);

}

void MainWindow::on_pushButton_ListInsert_clicked()
{
    QModelIndex index = ui->listView->currentIndex();
    model_s_l->insertRow(index.row());
    model_s_l->setData(index, "new item insert", Qt::DisplayRole);
    ui->listView->setCurrentIndex(index);
}

void MainWindow::on_pushButton_ListMoveUp_clicked()
{
    QModelIndex index;//列表中moveRow的parent没有意义

    int curRow = ui->listView->currentIndex().row();//当前行号
    model_s_l->moveRow(index, curRow, index, curRow - 1);//它本质是插入
    /*为了实现“上移”curRow，应当将curRow插入到curRow-1的上面
    * a b c -> b a   c -> b a c
    */
}

void MainWindow::on_pushButton_ListMoveDown_clicked()
{
    QModelIndex index;//列表中moveRow的parent没有意义

    int curRow = ui->listView->currentIndex().row();//当前行号
    model_s_l->moveRow(index, curRow, index, curRow + 2);//要加2
    /*加2的原因，moveRow本质是插入，为了实现“下移”curRow，应当将curRow插入到curRow+2的上面
    * 假设加1，例 a b c -> a    b c -> a b c
    * 加2，a b c -> a   c b -> a c b
    */
}

void MainWindow::on_pushButton_ListSort_clicked(bool checked)
{
    if (checked)
        model_s_l->sort(0, Qt::AscendingOrder);
    else
        model_s_l->sort(0, Qt::DescendingOrder);
}

void MainWindow::on_pushButton_TextClear_clicked()
{
    ui->plainTextEdit->clear();
}

void MainWindow::on_pushButton_TextImport_clicked()
{
    ui->plainTextEdit->clear();
    QStringList tmpList = model_s_l->stringList();
    for (int i = 0; i < tmpList.size(); i++)
    {
        ui->plainTextEdit->appendPlainText(tmpList.at(i));
    }
}

void MainWindow::on_listView_clicked(const QModelIndex& index)
{
    //%s不认识QString String，必须是C语言的以'\0'为结尾的字符串
    /*QString str = QString::asprintf("模型索引：row=%d，column=%d %s",
        index.row(),
        index.column(),
        model_s_l->data(index, Qt::DisplayRole).toString().toStdString());*/
    QString str = QString::asprintf("模型索引：row=%d，column=%d", index.row(), index.column());
    str += model_s_l->data(index, Qt::DisplayRole).toString();
    ui->statusBar->showMessage(str);
}




